<?php

namespace Addons\Card\Controller;

use Addons\Card\Controller\BaseController;

class ScoreController extends BaseController {
	
	function show(){
        if(isWeixinBrowser()){
            $map['uid'] = $this->mid;
            $res = M('card_member')->where($map)->find();
            if(!$res){
                redirect ( addons_url ( 'Card://Card/show' ) );
            }
            else{
                //获取签到标识
                $creditInfo = D('Common/Credit')->getLastCreditLog('member_checkin');
                if ($creditInfo) {
                    //判断是否今天
                    $cTime = intval($creditInfo['cTime']);
                    if (date('Y-m-d') === date('Y-m-d', $cTime)) {
                        $hasCheckin = true;
                    } else {
                        $hasCheckin = false;
                    }
                } else {
                    $hasCheckin = $creditInfo !== false;
                }

                if (IS_POST) {
                    if ($hasCheckin) {
                        exit('每天只能签到一次');
                    }
                    $credit = add_credit('member_checkin');
                    exit($credit);
                }
                $months = array();
                for($i=4; $i>=0; --$i) {
                    $months[] = array(
                        'year' => date('Y', strtotime('-'.$i. ' month')),
                        'month' => date('m', strtotime('-'.$i. ' month'))
                    );
                }
                //获取总微币
                $map['id'] = $this->mid;
                $map['token'] = get_token();
                $coin = M ( 'follow' )->where ( $map )->getField('score');
                !empty($coin) || $coin = 0;
                //获取总积分
                $map2['cli_card'] = get_card_number();
                $score = M ('client_card')->where($map2)->getField('cli_card_jf');
                !empty($score) || $score = 0;

                $this->assign('has_checkin', $hasCheckin);
                $this->assign('score', $score);
                $this->assign('coin', $coin);
                $this->assign('months', $months);

                $this->display();
            }
        }
        else{
            $this->ajaxReturn(array('msg'=>'请使用微信浏览器打开'));
        }
	}

    /**
     * 获取每月详细
     */
    function detail() {
        //获取当前月份记录
        $month = array('year'=> I('year', date('Y')), 'month'=> I('month',  date('m')));
        $monthRange = array(strtotime($month['year'].'-'.$month['month']), strtotime($month['year'].'-'.$month['month'].'-'.date('t').' 23:59:59'));
        $map3['uid'] = $this->mid;  //发布时取消注释
        $map3['cTime'] = array('between', $monthRange); //当月时间范围
        $model = M('credit_data');
//        $coinList = $model
//            ->Table('wp_credit_data data, wp_credit_config config')
//            ->field('FROM_UNIXTIME(cTime, \'%Y-%m-%d\') as cTime, title, data.score')
//            ->where('data.credit_name = config.name')
//            ->where('data.token = "' . get_token() . '"')
//            ->where($map3)
//            ->group('FROM_UNIXTIME(cTime, \'%Y-%m-%d\')')
//            ->order('cTime desc')
//            ->select();
        $coinList = $model
            ->table('wp_credit_data data')
            ->join('wp_credit_config config ON data.credit_name = config.name')
            ->field('FROM_UNIXTIME(cTime, \'%Y-%m-%d\') as cTime, cTime as time, title, data.score')
            ->where('data.token = "' . get_token() . '"')
            ->where($map3)
            ->order('time desc')
            ->select();

        !empty($coinList) || $coinList = array();
//        $map4['cli_card'] = array('EQ',get_card_number()); //发布时取消注释
//        $map4['cli_card_jf_date'] = array('LIKE', date('Y-m%')); //当月时间范围
//        $model= M('cli_card_jf');
//        $scoreList = $model
//            ->field('cli_card_jf_date as cTime, cli_card_jf_type as title, cli_card_jf_num')
//            ->where($map4)
//            ->order('cli_card_jf_date desc')
//            ->select();
//        !empty($scoreList) || $scoreList = array();
        $totalCoin = 0;
        foreach ($coinList as $c) {
            $totalCoin += intval($c['score']);
        }
        $this->ajaxReturn(array(
            'month' => $month,
            'total_coin' => $totalCoin,
//            'total_score' => $totalScore,
            'score_list' => $coinList
        ), 'JSON');
    }
    
    function scoreDataByDate($a,$b) {
        if ($a['cTime'] < $b['cTime']) {
            return 1;
        } else if ($a['cTime'] === $b['cTime']) {
            return 0;
        } else {
            return -1;
        }
    }

    /**
     * 每日详细
     */
    function dayDetail() {
        $type = I('get.type', 'coin');  //查看类型，coin微币，score积分
        $year = I('get.year', date('Y'));
        $month = I('get.month', date('m'));
        $day = I('get.day', date('d'));
        if ($type === 'coin') {
            $day_range = array("{$year}-{$month}-{$day} 00:00:00", "{$year}-{$month}-{$day} 23:59:59");
            $map['S.uid'] = $this->mid;  //发布时取消注释
            $map['S.token'] = get_token();
            $map['S.cTime'] = array('between', $day_range); //当日时间范围
            $model = M();
            $data_list = $model->table('wp_credit_data S')
                ->join('left join wp_credit_config C on S.credit_name=C.name')
                ->field('S.id,FROM_UNIXTIME(S.cTime, \'%Y-%m-%d\') as cTime, C.title, sum(S.score) as score')
                ->where($map)
                ->order('S.cTime desc')
                ->select();
        } else {
            $map['cli_card'] = array('EQ',get_card_number()); //发布时取消注释
            $map['cli_card_jf_date'] = array('LIKE', date('Y-m-d%')); //当日时间范围
            $model= M('cli_card_jf');
            $data_list = $model
                ->field('cli_card_jf_list_no as id,left(cli_card_jf_date, 10) as cTime,cli_card_jf_list_no as title, cast(sum(cli_card_jf_num) as int) as score')
                ->where($map)
                ->group('cli_card_jf_list_no')  //以订单号分组
                ->order('cli_card_jf_date desc')
                ->select();
        }
        !empty($data_list) || $data_list = array();
        $this->assign('param_data', array(
            'type' => $type,
            'year' => $year,
            'month' => $month,
            'day' => $day
        ));
        $this->assign('data_list', $data_list);
        // [{id,title,score,cTime}...]
        $this->display('day_detail');
    }

    /**
     * 线下消费订单详细
     */
//    function scoreDetail() {
//        $list_no = I('list_no');
//        if (empty($list_no)) {
//            $this->error('订单参数有误！');
//        }
//        $map['cli_card_jf_list_no'] = $list_no;
//    }

//    function test(){
//        $this->ajaxReturn(urlencode('http://www.sun-crystal.cn/index.php?s=/addon/Card/Card/storeList/token/gh_574a5ee591fe.html'));
//    }
//    function getaccess_token(){
//        $this->ajaxReturn(get_access_token());
//    }
//    function shorturl(){
//        $postdata = array('action'=>'long2short','long_url'=>'http://218.16.65.114/index.php?s=/addon/Card/Card/storeList.html');
//        $res = JsonPost('https://api.weixin.qq.com/cgi-bin/shorturl?access_token=' . get_access_token(), json_encode($postdata));
//        $resarr = json_decode($res);
//        $this->ajaxReturn($resarr);
//    }

    /*修正服务分数为0的信息*/
//    function evaluateupdate(){
//        $res1 = M('credit_comment')->where(array('score1'=>0))->save(array('score1'=>5));
//        $res2 = M('credit_comment')->where(array('score2'=>0))->save(array('score2'=>5));
//        $res3 = M('credit_comment')->where(array('score3'=>0))->save(array('score3'=>5));
//        $this->ajaxReturn(array('res1'=>$res1, 'res2'=>$res2, 'res3'=>$res3));
//    }
    // 微信上传坐标转百度坐标
//    function changepoint(){
//        $point = array('x'=>113.338844, 'y'=>23.175499);
//        $res = JsonGet('http://api.map.baidu.com/geoconv/v1/?coords=' . $point['x'] . ',' . $point['y'] . '&from=3&to=5&ak=zCDZ46TPskq5w4MVndrHBLq2');
//        $arr = json_decode($res, true);
//        if($arr['status'] == 0){
//            $baipoint['x'] = $arr['result'][0]['x'];
//            $baipoint['y'] = $arr['result'][0]['y'];
//        }
//        $this->ajaxReturn($baipoint);
//    }
//    function test(){
//        session_unset();
//    }
}
